﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using System.Windows.Forms;

namespace CallFlowDesigner
{
   
    public class ListViewColumnSorter : IComparer
    {
	    private int ColumnToSort;
	    private SortOrder OrderOfSort;
	    private CaseInsensitiveComparer ObjectCompare;
	    public ListViewColumnSorter()
	    {
		    ColumnToSort = 0;
            OrderOfSort = SortOrder.Ascending;
            ObjectCompare = new CaseInsensitiveComparer();
	    }

	    public int Compare(object x, object y)
	    {
		    int compareResult;
		    ListViewItem listviewX, listviewY;

		    listviewX = (ListViewItem)x;
		    listviewY = (ListViewItem)y;

		    // Compare the two items
		    compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,listviewY.SubItems[ColumnToSort].Text);
    			
		    if (OrderOfSort == SortOrder.Ascending)
		    {
			    return compareResult;
		    }
		    else if (OrderOfSort == SortOrder.Descending)
		    {
			    return (-compareResult);
		    }
		    else
		    {
			    return 0;
		    }
	    }
        
	    public int SortColumn
	    {
		    set
		    {
			    ColumnToSort = value;
		    }
		    get
		    {
			    return ColumnToSort;
		    }
	    }

	    public SortOrder Order
	    {
		    set
		    {
			    OrderOfSort = value;
		    }
		    get
		    {
			    return OrderOfSort;
		    }
        }
    }
}
